/**
 * 风险评测
 * Created by KY on 2017/8/10.
 */
define(['ldfw/core','text!tpl/risk_assessment.tpl','comps/session','comps/ajax',
    './RiskResultView'],
    function(LDFW,tpl,session,ajax,riskResultView){
    let View = LDFW.Container.extend({
        tpl:tpl,
        className:'panel',
        events:{
          'click #submitQuestion':'submitQuestion'
        },
        initialize:function(){
            this.on('shown',this.loadData)
        },
        loadData:function(param){
            const _this = this
            let curr_cust = session.getCurrentUser()
            let paper_type = curr_cust.cust_type === 1?2:1
            $.when(ajax.get('getPaper.do',{paper_type:paper_type}),ajax.post('cpliL.1.57',{cust_type:curr_cust.cust_type}))
                .done(function(xhr1,xhr2){
                    let questions = xhr1[0].questions,
                        answers = xhr2[0].result;
                    if(!_.isEmpty(questions) && !_.isEmpty(answers)){
                        questions.forEach(function(question,index){
                            if(answers[index] && question.quest_content === answers[index].quest_content){
                                question.opt_value = answers[index].opt_value
                            }
                        })
                    }
                    _this.model.set('questions',questions)
                    _this.paper_no = xhr1[0].paper_no
                    _this.render()
                })
        },
        submitQuestion:function(formData){
            let data = ''
            this.$el.find('input[type=radio]:checked').each(function(index,el){
                data = data + $(el).attr('name')+','+$(el).val()+';'
            })
            ajax.post('cpliL.1.15',{
                qst_ans_str:data,
                paper_no:this.paper_no
            }).success(function(resp){
                riskResultView.setRiskResult({
                    answer_id:resp.result.answer_id,
                    low_risk_flag:resp.result.low_risk_flag,
                    ques_invest_num:resp.result.ques_invest_num,
                    risk_accept:resp.result.risk_accept
                })
                LDFW.navigate('risk_result')
            })
        }
    })
    return new View()
})